package b_ouput;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 测试文件输出流的示例类
 * @author Cheng-02
 * @version V1.0
 * @Package b_ouput
 * @date 2024/4/12 下午3:19
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        // 调用方法2，演示写入字节数组的一部分

      method4();
    }

    private static void method4() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("module10\\2.txt",true);

        fileOutputStream.write("abc\n".getBytes());
        fileOutputStream.close();
    }

    private static void method3() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("module10\\2.txt");

        fileOutputStream.write("abc".getBytes());

    }

    /**
     * 演示写入字节数组的一部分到文件
     */
    private static void method2() {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("module10\\2.txt");
            // 将字节数组arr从索引2开始的1个字节写入文件
            byte[] arr = {97, 98, 99, 100, 111, 23,};
            fileOutputStream.write(arr, 2, 1);
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            // 文件未找到异常处理
            throw new RuntimeException(e);
        } catch (IOException e) {
            // 输入/输出异常处理
            throw new RuntimeException(e);
        }
    }

    /**
     * 演示写入整个字节数组到文件
     */
    private static void method1() {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("module10\\2.txt");
            // 将字节数组arr全部写入文件
            byte[] arr = {97, 98, 99, 100, 111, 23,};
            fileOutputStream.write(arr);
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 演示写入单个字节到文件
     */
    private static void method() {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("module10\\2.txt");
            // 将单个字节97写入文件
            fileOutputStream.write(97);
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
